package com.base.invoke.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.base.invoke.annotation.DesensitizeAnno;
import com.base.invoke.entity.CardInfo;
import org.apache.commons.lang3.StringUtils;

/**
 * 脱敏工具类（通过注解）
 * 且通过此类的日志打印信息，才能显示脱敏信息
 * @author coolpig
 * @date 2019-09-30
 */
public class DesensitizeUtils {

    private DesensitizeUtils() {

    }

    /**
     *
     * @Title: toJsonString
     * @Description: 日志输出
     * @param object
     * @return
     */
    public static String toJsonString(Object object) {
        return JSON.toJSONString(object, getValueFilter());
    }

    /**
     *
     * @Title: getValueFilter
     * @Description: 日志脱敏主方法
     * @return
     */
    private static final ValueFilter getValueFilter() {
        return (Object obj, String key, Object value) -> {
            try {
                DesensitizeAnno annotation = obj.getClass().getDeclaredField(key).getAnnotation(DesensitizeAnno.class);
                if (null != annotation && value instanceof String) {
                    String strVal = (String) value;
                    if (StringUtils.isNotBlank(strVal)) {
                        switch (annotation.type()) {
                            case PHONE:
                                return mobilePhone(strVal);
                            case ID_CARD:
                                return idCardNum(strVal);
                            case BANK_CARD:
                                return bankCard(strVal);
                            case NAME:
                                return desensitizeName(strVal);
                            case EMAIL:
                                return desensitizeEmail(strVal);
                            default:
                                break;
                        }
                    }
                }
            } catch (NoSuchFieldException | SecurityException e) {
            }
            return value;
        };
    }

    /**
     *
     * @Title: mobilePhone
     * @Description: [手机号码] 前三位，后两位，其他隐藏<例子:138********34>
     * @param num
     * @return
     * @throws
     */
    public static String mobilePhone(String num) {
        if (StringUtils.isBlank(num)) {
            return "";
        }
        return StringUtils.left(num, 3).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(num, 2), StringUtils.length(num), "*"), "***"));
    }

    /**
     *
     * @Title: idCardNum
     * @Description:  [身份证号] 显示前六位，最后两位，其他隐藏。共计18位或者15位。<例子：152728*********62>
     * @param num
     * @return
     * @throws
     */
    public static String idCardNum(String num) {
        if (StringUtils.isBlank(num)) {
            return "";
        }
        return StringUtils.left(num, 6).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(num, 2), StringUtils.length(num), "*"), "******"));
    }

    /**
     *
     * @Title: bankCard
     * @Description:[银行卡号] 前六位，后四位，其他用星号隐藏每位1个星号<例子:6222600**********1234>
     * @param cardNum
     * @return
     * @throws
     */
    public static String bankCard(String cardNum) {
        if (StringUtils.isBlank(cardNum)) {
            return "";
        }
        return StringUtils.left(cardNum, 6).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(cardNum, 4), StringUtils.length(cardNum), "*"), "******"));
    }

    /**
     *
     * @Title: desensitizeEmail
     * @Description:[电子邮箱] 邮箱前缀仅显示第一个字母，前缀其他隐藏，用星号代替，@及后面的地址显示<例子:g**@163.com>
     * @param email
     * @return
     * @throws
     */
    public static String desensitizeEmail(String email) {
        if (StringUtils.isBlank(email)) {
            return "";
        }
        int index = StringUtils.indexOf(email, "@");
        if (index <= 1) {
            return email;
        } else {
            return StringUtils.rightPad(StringUtils.left(email, 1), index, "*").concat(StringUtils.mid(email, index, StringUtils.length(email)));
        }
    }

    /**
     *
     * @Title: desensitizeName
     * @Description: [中文姓名] 只显示第一个汉字，其他隐藏为2个星号<例子：李**>
     * @param fullName
     * @return
     * @throws
     */
    private static final Object desensitizeName(String fullName) {
        if (StringUtils.isBlank(fullName)) {
            return "";
        }
        String name = StringUtils.left(fullName, 1);
        return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
    }

    public static void main(String[] args) {
        CardInfo cardInfo = new CardInfo();
        cardInfo.setCardId("100221198612165021");
        cardInfo.setName("张大大");
        cardInfo.setPhone("18600332677");
        cardInfo.setBank("622644448888777999");
        System.out.println(DesensitizeUtils.toJsonString(cardInfo));
        System.out.println(JSON.toJSONString(cardInfo));
    }
}
